/*
**  Copyright (c) 2014 Michael Liu(michael.liu.point@gmail.com).
**
**  Project: Gateway Unified Management Platform
**  File:    dpcmn.c
**  Author:  Michael
**  Date:    06/24/2014
**
**  Purpose:
**    data path common methods.
*/

/* Include files. */

#include "dpmgr.h"
#include "svcmgr.h"
#include "osdep.h"
#include "pfdep.h"

/* Macro constant definitions. */

/* Type definitions. */

/* Local function declarations. */

/* Macro API definitions. */

/* Global variable declarations. */

e_ret dp_cmn_proc_req(tdata_paths * dp, tcm_dp_req_body * req, tcm_dp_rsp_body ** rsp, int *size)
{
	e_ret ret = eRET_SUCCESS;
	int rsp_size;
	unsigned char opt;
	tcm_dp_rsp_body *new_rsp;
	tcm_dp_cmn_req_body *cmn_req;
	tcm_dp_cmn_rsp_body *cmn_rsp;
	tcm_dp_cmn_rsp_body cmn_rsp_body;

	sysmgr_trace_enter();

	rsp_size = 0;
	new_rsp = NULL;

	cmn_req = (tcm_dp_cmn_req_body *) req->body;
	opt = cmn_req->opt;

	sysmgr_dbg("dp_cmn_proc_req() opt[%d]\n", opt);
	switch (opt) {
	case eDP_CMN_OPT_GET_DP_MODE:
		memset(&cmn_rsp_body, 0, sizeof(cmn_rsp_body));
		cmn_rsp_body.mode = dp->mode;
		ret = eRET_SUCCESS;
		break;

	case eDP_CMN_OPT_SET_DP_MODE:
		ret = pf_dep_set_device_mode(cmn_req->mode);
		if (ret == eRET_SUCCESS) {
			dp->mode = cmn_req->mode;
			cmn_rsp_body.mode = dp->mode;
		}
		break;

	case eDP_CMN_OPT_SET_COUNTRY:
		ret = pf_dep_set_country(dp, cmn_req->country_number);
		sysmgr_dbg("pf_dep_set_country() ret[%d]\n", ret);
		break;

	case eDP_CMN_OPT_GET_COUNTRY:
		ret = pf_dep_get_country(&cmn_rsp_body.country_number);
		sysmgr_dbg("pf_dep_get_country() ret[%d]\n", ret);
		break;

	case eDP_CMN_OPT_GET_DEVMAC:
		ret = pf_dep_get_devmac(&cmn_rsp_body.devmac);
		sysmgr_dbg("pf_dep_get_devmac() ret[%d]\n", ret);
		break;

	default:
		break;
	}

	rsp_size = sizeof(*new_rsp) + sizeof(*cmn_rsp);
	new_rsp = malloc(rsp_size);
	if (new_rsp == NULL) {
		sysmgr_err("malloc(%d) failed, err: %d,[%s]\n", rsp_size, errno, strerror(errno));
		ret = eRET_NO_RESOURCE;
		goto out;
	}

	cmn_rsp = (tcm_dp_cmn_rsp_body *) new_rsp->body;
	memcpy(cmn_rsp, &cmn_rsp_body, sizeof(*cmn_rsp));;

out:
	*size = rsp_size;
	*rsp = new_rsp;

	sysmgr_trace_exit();
	return (ret);
}
